package com.cloudbroker.bcs.platform.dav.common.util;

import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.cloudbroker.bcs.common.util.ValueUtil;
import com.cloudbroker.bcs.platform.dav.common.constant.HttpConstant;
import com.cloudbroker.bcs.platform.dav.common.util.ssl.SSLClient;

/**
 * Created by xuyn15828 on 2016/5/19.
 */
public class HttpsUtil {
    
    public static String get(String url) {
        HttpClient httpClient = null;
        String result = null;
        try {
            httpClient = new SSLClient();
            HttpGet get = new HttpGet(url);
            
            HttpResponse response = httpClient.execute(get);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, "utf-8");
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
    
    public static String doGet(String url, Map<String, Object> params, String charSet, String authorization) {
        HttpClient httpClient = null;
        String result = null;
        try {
            if (params != null && !params.isEmpty()) {
                url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(convertPairs(params), charSet));
            }
            httpClient = new SSLClient();
            HttpGet get = new HttpGet(url);
            get.setHeader("Content-Type", HttpConstant.CONTENT_TYPE_FORM);
            get.setHeader("Authorization", authorization);
            HttpResponse response = httpClient.execute(get);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charSet);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
    
    public static List<NameValuePair> convertPairs(Map<String, Object> params) {
        if (null != params && !params.isEmpty()) {
            List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                String value = ValueUtil.getString(entry.getValue());
                if (value != null) {
                    pairs.add(new BasicNameValuePair(entry.getKey(), value));
                }
            }
            return pairs;
        }
        return null;
    }
    
    public static Map<String, Object> beanToMap(Object obj) {
        Map<String, Object> params = new HashMap<String, Object>(0);
        try {
            PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
            PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(obj);
            for (int i = 0; i < descriptors.length; i++) {
                String name = descriptors[i].getName();
                if (!"class".equals(name)) {
                    params.put(name, propertyUtilsBean.getNestedProperty(obj, name));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return params;
    }
}
